<?php

/**
 * OpenApi 接口控制器, 允许外部程序访问, 使用jwt token 验证
 *
 * @author pufan
 */

namespace Api\Controller;
use Think\Controller;

use Think\Log;
use \Org\Util\Jwt;
use Library\CardCode;

class OpenApiController extends Controller {
    function _initialize() {
        if (!IS_POST) { //禁止get访问
            exit;
        }
        if ($this->verification() == false) {
            $this->api_return('401', 'unauthorized');
        }
    }
    /*卡券核销*/
    public function getCardDetail() {
        $code = $_POST['cardCode'];
        if (!$code) {
            $this->api_return(1, 'Parameter error');
        }

        $card = CardCode::getInstance();
        $cardDetail = $card->getCardDetail('', $code);
        if (!$cardDetail) {
            $this->api_return(2, 'fail');
        }
        unset($cardDetail['id']);
        unset($cardDetail['card_intro']);
        unset($cardDetail['winning_time']);
        unset($cardDetail['card_icon']);
        unset($cardDetail['open_time_type']);
        unset($cardDetail['open_time_day']);
        unset($cardDetail['open_time_period']);
        $this->api_return(0, 'ok', $cardDetail);
    }

    /*卡券核销*/
    public function cardVerification() {
        $code = $_POST['cardCode'];
        if (!$code) {
            $this->api_return(1, 'Parameter error');
        }
        $card = CardCode::getInstance();
        $result = $card->checkConsume('', $code);

        //无效的卡券
        if ($result == 2) {
            $this->api_return(2, '卡券已经使用');
        }

        //无效的卡券
        if (!$result || $result != 1) {
            $this->api_return(3, '无效的卡券');
        }

        $result = $card->consume('', $code);

        if ($result) {
            $this->api_return(0, 'ok');
        } else {
            $this->api_return(4, '核销失败');
        }
    }

    /*验证*/
    private function verification() {
        $result = FALSE;
        $accessToken = $_SERVER['HTTP_ACCESS_TOKEN'];  //Access-Token
        \Think\Log::record('verification: '. json_encode($accessToken));

        if ($accessToken) {
            try {
                $jwt = new Jwt();
                $payload_out = $jwt->decode($accessToken, C('JWT_KET'));
                \Think\Log::record('verification: '.$payload_out);
                if ($payload_out) {
                    $payload = json_encode($payload_out, true);
                    if ($payload['iat'] && $payload['exp']) {
                        $result = true;
                    }
                }
            } catch (\Exception $e) {
                \Think\Log::record('verification: catch Exception'.$e->getMessage());
            }
        }
        return $result;
    }

    protected function api_return($errcode = 0, $errmsg = 'ok', $data = array(), $data_name='data') {
        $json_data['errcode'] = $errcode;
        $json_data['errmsg'] = $errmsg;

        if ($data) {
            $json_data[$data_name] = $data;
        }

        \Think\Log::record('query_string: '.$_SERVER['QUERY_STRING'].' args:'.json_encode($_POST).' '.'api_return:'. json_encode($json_data));
        $this->ajaxReturn($json_data);
    }
}

